# -*- coding: utf-8 -*-

import json

from django.shortcuts import render, HttpResponse
from django.views.generic.base import View
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger

from utils.mixin_utils import LoginRequiredMixin
from .models import Course, Lesson, ChoiceQuestion, Video, ProgramQuestion, ChoiceBank, ProgramUpload, CourseCategory
from .models import CourseDirection
from operation.models import UserCourse
from integral.models import UserIntergral, IntergralDemand
from .forms import ProgramUploadForm


# Create your views here.


class CourseListView(View):
    """
    课程列表页
    """
    def get(self, request):
        all_category = CourseCategory.objects.all()
        all_direction = CourseDirection.objects.all()
        click_direction = request.GET.get("direction", "all")
        click_category = request.GET.get("category", "all")
        click_degree = request.GET.get("degree", "all")
        click_sort = request.GET.get("sort", "new")
        print(click_direction)

        if click_direction == "all":
            if click_category == "all":
                if click_degree == "all":
                    all_course = Course.objects.all().order_by("-add_time")
                    if click_sort == "hot":
                        all_course = Course.objects.all().order_by("-students")
                else:
                    all_course = Course.objects.filter(degree=click_degree).order_by("-add_time")
                    if click_sort == "hot":
                        all_course = all_course.order_by("-students")
            else:
                if click_degree == "all":
                    all_course = Course.objects.filter(coursecategory__category=click_category).order_by("-add_time")
                    if click_sort == "hot":
                        all_course = all_course.order_by("-students")
                else:
                    all_course = Course.objects.filter(coursecategory__category=click_category, degree=click_category).order_by("-add_time")
                    if click_sort == "hot":
                        all_course = all_course.order_by("-students")
        else:
            all_course = Course.objects.filter(direction=click_direction)
            print("all:",all_course)
            if click_category == "all":
                if click_degree == "all":
                    all_course = all_course.order_by("-add_time")
                    if click_sort == "hot":
                        all_course = all_course.order_by("-students")
                else:
                    all_course = all_course.filter(degree=click_degree).order_by("-add_time")
                    if click_sort == "hot":
                        all_course = all_course.order_by("-students")
            else:
                if click_degree == "all":
                    all_course = all_course.filter(coursecategory__category=click_category).order_by("-add_time")
                    if click_sort == "hot":
                        all_course = all_course.order_by("-students")
                else:
                    all_course = all_course.filter(coursecategory__category=click_category, degree=click_category).order_by("-add_time")
                    if click_sort == "hot":
                        all_course = all_course.order_by("-students")
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1

        p = Paginator(all_course, 6, request=request)

        courses = p.page(page)
        print(type(courses))

        print("session:", request.session.get('Linux', default="Html"))

        return render(request, 'course_list.html', {
            "all_direction": all_direction,
            "all_course": courses,
            "all_category": all_category,
            "click_direction": click_direction,
            "click_category": click_category,
            "click_degree": click_degree,
            "click_sort": click_sort,
        })


class CourseLevelView(View):
    """
    课程关卡页
    """

    def get(self, request, course_id):
        course = Course.objects.get(id=int(course_id))
        # 查询用户是否已经关联了该课程
        user_courses = UserCourse.objects.filter(user=request.user, course=course)
        if not user_courses:
            user_course = UserCourse(user=request.user, course=course)
            user_course.save()

        students = UserCourse.objects.all().count()
        course.students = int(students)
        course.save()

        all_level = Lesson.objects.filter(course=course_id).order_by("add_time")
        this_level = Lesson.objects.filter(course=course_id).first()
        # # 下一关关卡
        # try:
        #     next_level = Lesson.objects.filter(course=course_id).order_by("add_time")[Level_num+1]
        # except IndexError:
        #     next_level = Lesson.objects.filter(course=course_id).order_by("add_time")[Level_num]
        #
        # # 开通下一关关卡
        # if this_level.pass_level:
        #     next_level.pass_level = True

        last_level = Lesson.objects.filter(course=course_id).last()

        return render(request, 'course_level.html', {
            "course": course,
            "all_level": all_level,
            "courseid": course_id,
            "last_level": last_level,
        })


class CourseDetailView(View):
    """
    课程详情页
    """

    def get(self, request, course_id, lesson_id):
        course = Course.objects.get(id=course_id)

        lesson = Lesson.objects.get(id=lesson_id)
        print(lesson)

        # user_intergral = UserIntergral.objects.get(user=request.user)
        extend_demand = IntergralDemand.objects.get(lesson_id=int(lesson_id), demand='extend_download')
        # explain_demand = IntergralDemand.objects.get(lesson_id=int(lesson_id), demand='pro_explain')

        all_vedio = Video.objects.filter(lesson_id=lesson_id)
        return render(request, 'course_detail.html', {
            "course": course,
            "lesson": lesson,
            "all_vedio": all_vedio,
            "extend_demand": extend_demand,
        })


class ChoiceQuestionView(View):
    """
    选择题题目
    """

    def get(self, request, course_id, lesson_id, choice_id):
        lesson_choices = ChoiceQuestion.objects.filter(lesson_id=int(lesson_id))
        this_question = ChoiceQuestion.objects.get(id=choice_id)
        all_question_num = ChoiceQuestion.objects.filter(lesson_id=int(lesson_id)).count()
        print(all_question_num)
        if int(choice_id) == 1:
            request.session['right_count'] = 0
        is_last = False
        if int(choice_id) == all_question_num:
            is_last = True

        return render(request, 'choice_pra.html', {
            "lesson_choices": lesson_choices,
            "is_last": is_last,
            "this_question": this_question,
            "choice_id": choice_id,
            "lesson_id": lesson_id,
            "course_id": course_id,
        })


class NextQuestionView(View):
    """
    下一题
    """

    def post(self, request):
        this_question = request.POST.get("practice_num", 1)
        user_answer = request.POST.get("user_answer", "")
        next_question = int(this_question) + 1
        if int(user_answer) != -1:
            # 得到本题的正确答案
            right = ChoiceQuestion.objects.get(id=int(this_question))
            right_answer = right.answer
            if int(user_answer) + 1 == right_answer:
                print("答对本题")
                request.session['right_count'] = request.session.get('right_count', default=0) + 1
            else:
                print("本题错误")
                # error_question = ErrorQuestion()
                # error_question.user = request.user
                # choicequestion = ChoiceQuestion.objects.get(id=int(choice_id))
                # choice = Choice.objects.get(id=int(this_question))
                # try:
                #     ErrorQuestion.objects.get(user=request.user, choicequestion=choicequestion, choice=choice)
                # except ErrorQuestion.DoesNotExist:
                #     error_question.choicequestion = choicequestion
                #     error_question.choice = choice
                #     error_question.save()
            value = {"status": "success", "next_question": next_question}
            print("session", request.session['right_count'])
            return HttpResponse(json.dumps(value), content_type='application/json')
        else:
            return HttpResponse('{"status":"fail", "msg":"没有进行选择"}', content_type='application/json')


class ChoiceResultView(View):
    """
    选择题结果
    """

    def get(self, request, course_id, lesson_id):
        right_nums = request.session.get('right_count', default=0)
        print("right_nums:", right_nums)
        all_question_num = ChoiceQuestion.objects.filter(lesson_id=int(lesson_id)).count()
        print("all_num:", all_question_num)
        right_rate = int(int(right_nums) / float(all_question_num) * 100)
        print(right_rate)

        return render(request, 'choice_result.html', {
            "course_id": course_id,
            "lesson_id": lesson_id,
            "right_nums": right_nums,
            "right_rate": right_rate,
        })


class ProgramView(View):
    """
    编程题
    """

    def get(self, request, course_id, lesson_id, program_id):
        # program_file = ProgramQuestion.objects.get(course=int(course_id), lesson=int(lesson_id), id=int(program_id))

        try:
            program_result = ProgramUpload.objects.get(programquestion_id=int(program_id), user=request.user)
        except ProgramUpload.DoesNotExist:
            program_result = ProgramUpload.objects.all()
        print(program_result)

        return render(request, 'program.html', {
            # "program_file": program_file,
            "program_result": program_result,
            "program_id": program_id,
            "course_id": course_id,
            "lesson_id": lesson_id,
        })


class ProgramCommitView(View):
    """
    编程题
    """

    def get(self, request, course_id, lesson_id, program_id):
        # program_file = ProgramQuestion.objects.get(course=int(course_id), lesson=int(lesson_id), id=int(program_id))

        try:
            program_result = ProgramUpload.objects.get(programquestion_id=int(program_id), user=request.user)
        except ProgramUpload.DoesNotExist:
            program_result = ProgramUpload.objects.all()
        print(program_result)

        return render(request, 'program-commit.html', {
            # "program_file": program_file,
            "program_result": program_result,
            "program_id": program_id,
        })


class ProgramUploadView(View):
    """
    编程项目上传
    """

    def post(self, request):
        file_obj = request.FILES.get('file')
        image_obj = request.FILES.get('image')
        learned_obj = request.POST.get('learn')
        programId_obj = request.POST.get('programId')
        program = ProgramQuestion.objects.get(id=int(programId_obj))
        # user = request.user
        if file_obj and image_obj:
            program_result = ProgramUpload()
            program_result.programquestion = program
            program_result.user = request.user
            program_result.upload = file_obj
            program_result.image = image_obj
            program_result.learned = learned_obj
            program_result.is_complete = True
            program_result.save()

            return HttpResponse('{"status":"success"}', content_type='application/json')
        else:
            return HttpResponse('{"status":"fail"}', content_type='application/json')


class PostView(View):
    def post(self, request):
        import time
        import os
        from programing import settings
        file_obj = request.FILES.get('file')
        image_obj = request.FILES.get('image')
        if file_obj:  # 处理附件上传到方法
            request_set = {}
            print('file--obj', file_obj)
            # user_home_dir = "upload/%s" % (request.user.userprofile.id)
            accessory_dir = settings.BASE_DIR
            if not os.path.isdir(accessory_dir):
                os.mkdir(accessory_dir)
            upload_file = "%s/%s" % (accessory_dir, file_obj.name)
            recv_size = 0
            with open(upload_file, 'wb') as new_file:
                for chunk in file_obj.chunks():
                    new_file.write(chunk)
            order_id = time.strftime("%Y%m%d%H%M%S", time.localtime())
            # cache.set(order_id, upload_file)
            return HttpResponse(order_id)


class CompleteView(View):
    """
    关卡完成
    """

    def post(self, request):
        course_id = request.POST.get("course_id", 1)
        lesson_id = request.POST.get("lesson_id", 1)
        print(type(int(course_id)), int(course_id))
        this_lesson = Lesson.objects.get(course_id=int(course_id), id=int(lesson_id))
        print(this_lesson)
        this_lesson.pass_level = True
        last_level = Lesson.objects.filter(course=int(course_id)).last()
        print(last_level)

        choice_bank = ChoiceBank.objects.get(lesson=int(lesson_id))
        print(choice_bank)
        program_question = ProgramQuestion.objects.get(lesson=int(lesson_id))

        if choice_bank.is_complete == True and program_question.is_complete == True:
            if int(lesson_id) != last_level.id:
                next_level = Lesson.objects.filter(course=int(course_id)).order_by("-add_time")[int(lesson_id) + 1]
                print("next:", next_level)
                next_level.open_level = True
                this_lesson.pass_level = True
                this_lesson.save()
                next_level.save()
            else:
                this_lesson.pass_level = True
                this_lesson.save()

            value = {"status": "success", "msg": "已完成"}
            return HttpResponse(json.dumps(value), content_type='application/json')
        else:
            value = {"status": "fail", "msg": "你还没有完成全部任务"}
            return HttpResponse(json.dumps(value), content_type='application/json')


class ProjectShowView(View):
    """
    项目展示
    """

    def get(self, request):
        all_category = CourseCategory.objects.all()

        click_category = request.GET.get("category", "all")
        click_course = request.GET.get("course", "all")
        click_level = request.GET.get("level", "all")
        all_level = Lesson.objects.filter(course__name=click_course)

        if click_category == "all":
            print("category:", click_category)
            all_project = ProgramUpload.objects.filter(is_show=True).order_by("-add_time")
            all_course = Course.objects.filter()
        else:
            all_course = Course.objects.filter(coursecategory__category=click_category)
            if click_course == "all":

                all_project = ProgramUpload.objects.filter(lesson__course__coursecategory__category=click_category,
                                                           is_show=True)
            else:
                if click_level == "all":

                    all_project = ProgramUpload.objects.filter(lesson__course__coursecategory__category=click_category,
                                                               lesson__course__name=click_course,
                                                               is_show=True)
                else:

                    all_project = ProgramUpload.objects.filter(lesson__course__coursecategory__category=click_category,
                                                               lesson__course__name=click_course,
                                                               lesson__name=click_level,
                                                               is_show=True)
        # 对课程进行分页
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1

        p = Paginator(all_project, 6, request=request)
        projects = p.page(page)

        return render(request, "project_show.html", {
            "all_category": all_category,
            "click_category": click_category,
            "all_course": all_course,
            "click_course": click_course,
            "all_level": all_level,
            "click_level": click_level,
            "projects": projects,

        })


class ProjectResultView(View):
    """
    项目展示结果
    """

    def get(self, request, lesson):
        try:
            program_result = ProgramUpload.objects.get(lesson__name=lesson, user=request.user)
        except ProgramUpload.DoesNotExist:
            program_result = ProgramUpload.objects.all()
        print(program_result)
        return render(request, "project_result.html", {
            "program_result": program_result
        })


class DownloadUrlView(View):
    """链接下载"""
    def post(self, request):
        course_id = request.POST.get("course_id", 1)
        lesson_id = request.POST.get("lesson_id", 1)
        type = request.POST.get("type", "")
        user_intergral = UserIntergral.objects.get(user=request.user)
        demand_intergral = IntergralDemand.objects.get(lesson_id=int(lesson_id), demand=type)
        if user_intergral.grade >= demand_intergral.intergral:
            user_intergral.grade = user_intergral.grade - demand_intergral.intergral
            user_intergral.save()
            demand_intergral.download_count += 1
            demand_intergral.save()
            value = {"status": "success", "re_url": demand_intergral.url}
            return HttpResponse(json.dumps(value), content_type='application/json')
        else:
            value = {"status": "fail", "msg": "您的积分不足，请充值!"}
            return HttpResponse(json.dumps(value), content_type='application/json')
